// ext实例展示的js文件
Ext.BLANK_IMAGE_URL = '/static/images/default/s.gif';
Ext.QuickTips.init();//加载快速提示框
Ext.namespace('Andy.ux')
Andy.ux.IFrameComponent = Ext.extend(Ext.BoxComponent, {
     onRender : function(ct, position){
          this.el = ct.createChild({tag: 'iframe', id: 'iframe-'+ this.id, frameBorder: 0, src: this.url});
     }
});

Ext.namespace('Andy.app');//创建名字域
var gridPanel;
var projectGridPanel;
//定义数对象
function TreeNode(treeId,treeName,treeDataUrl){
	this.treeId = treeId;
	this.treeName = treeName;
	this.treeDataUrl = treeDataUrl;
}
var configFormWindow;
var configFormPanel;
var uploadFormWindow;
var uploadFormPanel;


var recordDef = Ext.data.Record.create([{name:'code'},{name:'name'}]);
var dataStore = new Ext.data.Store({
	proxy:new Ext.data.HttpProxy({url:'/getAllEnviroments'}),
	reader:new Ext.data.JsonReader({id:'code',root:'comboBox'},recordDef),
	remoteStore:true
});

var projectDataStore = new Ext.data.Store({
	proxy:new Ext.data.HttpProxy({url:'/getApplications'}),
	reader:new Ext.data.JsonReader({id:'code',root:'comboBox'},recordDef),
	remoteStore:true
});

//应用程序主页面
Andy.app = function(){
	//空函数
};
var treeSelecedId = null;
var loadedResourceId = null;
var loadedWorkMenu = null;
var loadedTabs = new Array();
Ext.extend(Andy.app,Ext.util.Observable,{
	//导航菜单区
	andyView:null,
	accordion: new Ext.Panel({
                region:'west',
                autoScroll:true, 
				enableDD:false,//是否支持拖拽效果
				containerScroll: true,//是否支持滚动条
				collapsible:true,//是否让panel能自动缩放
				collapseMode:'mini',//在分割线处出现按钮
				split:true,
                margins:'2 2 2 2',
                split:true,
                id:'accordionPanel',
                width: 220,
                layout:'accordion',
                layoutConfig: {
            		titleCollapse: true,
            		animate: true,
            		ctiveOnTop: false
        		}
    }),
	//主要显示区
    main:new Ext.TabPanel({   
		region:'center',   
		enableTabScroll:true,   
		activeTab:0,
		margins:'0 2 2 0',
		items:[
			   	new Ext.Panel({   
					id:"workPing",
					title:"配置变更列表",
					layout:'fit', 
					border:false ,
					items: [ new Andy.ux.IFrameComponent({ id: 'workPing', url: '/configLogsManage' }) ]
               	})
			  ]   
	}),  
	//初始化构造函数
	init:function(trees){
		//初始化界面
		this.andyView = new Ext.Viewport({
			layout:'border',
			border:false,
			items:[{
                    region:'north',
                    split:false,
                    height:60,
                    collapsible: false,
                    html: headLinkHtml,
                    cls : '',
                    margins:'0 0 0 0'
                },this.main,this.accordion]
		});
		//this.createGridPanel();
		//this.createProjectGridPanel();
		this.main.on('beforeremove',this.fixIFrame,this);
		this.andyView.render();
		//新建一个mask
		//this.loadMask = new Ext.LoadMask(this.main.body,{msg:"页面加载中……"});
		initUserStatus();
	},
	fixIFrame:function(o, p,e) {
		var iFrame = p.getEl().dom; 
		if (iFrame.src) { 
			iFrame.src = "javascript:false"; 
		} 
	},
	loadWorkMenu:function(resourceId,resourceName){
		if( loadedResourceId!=null && loadedResourceId==resourceId){
			return;
		}
		//清除原来的内容，增加新设置的内容
		//this.accordion.remove();
		var workMenu = this.createMenuTree("000000","配置管理",systemContext + '/getMainPageMenu',resourceId);
		if( loadedWorkMenu!=null ){
			this.accordion.remove(loadedWorkMenu,true);
		}
		this.accordion.add(workMenu);
		loadedResourceId = resourceId;
		loadedWorkMenu = workMenu;
		this.andyView.render();
		workMenu.getRootNode().expand(true,true);
		while( loadedTabs.length>0 ){
			var tab = this.main.getComponent(loadedTabs.shift());//得到tab组建
			if( tab ){
				this.main.remove(tab,true);
			}
		}
	},
	reloadWorkMenu:function(){
		//重新加载工作子目录
		if( loadedWorkMenu!=null ){
			loadedWorkMenu.root.reload();
			loadedWorkMenu.root.expand(true);
		}
	},
	createMenuTree:function(treeId,treeTitle,treeDataUrl,resourceId){
		var treeloader = new Ext.tree.TreeLoader({  
	        dataUrl : treeDataUrl
	    });
		treeloader.on("beforeload", function(treeLoader, node) {  
	        treeLoader.baseParams.resourceId = node.id;    
	        treeLoader.baseParams.node_text = node.text;    
	    }, this);
		var menuTree = new Ext.tree.TreePanel({
			title:treeTitle,
			id:'' + treeId,
			autoScroll:true, 
			enableDD:false,//是否支持拖拽效果
			containerScroll: true,//是否支持滚动条
			split:true,
	        width: 240,
	        minSize: 175,
	        maxSize: 300,
			collapseMode:'mini',//在分割线处出现按钮
	        collapsible: true,
	        margins:'0 0 0 0',
	        boder:false,
	        defaults: {
				bodyStyle: 'padding:15px;color:red'
			},
	        //rootVisible:false,
			loader:treeloader,
			tools:[{
					id:'refresh',
					handler:function(){
						var tree = Ext.getCmp(treeId);
						tree.root.reload();
					}
				}]
		});
	
		//菜单根节点
		var menuRoot = new Ext.tree.AsyncTreeNode({
			text:treeTitle,
			draggable:false,
			text:'配置管理中心',
			id:treeId
		});
		
		menuTree.setRootNode(menuRoot);
		//menuRoot.expand();
		
		this.accordion.add(menuTree);
		
		//给main的tab页面添加tabchange事件
		this.main.on('tabchange', this.changeTab, this);
		
		//给树形菜单添加事件
		menuTree.on('click',this.menuClickAction,this);
		return menuTree;
	},
	//属性菜单的点击事件
	menuClickAction:function(node){
		if(!node.isLeaf()){   
            return false;   
        }
		var tabId = 'tab-' + node.id;
		var tabTitle = node.text;
		var tabTitle = node.text;
		var tab = this.main.getComponent(tabId);//得到tab组建
		if(!tab){
			tab = this.main.add(new Ext.Panel({
			     id: tabId,
			     title: tabTitle,
			     closable:true,
			     layout:'fit', 
			     html:'<iframe src="" name="' + tabId + '1" id="' + tabId + '1" width="100%" marginwidth="0" height="100%" marginheight="0" scrolling="auto" frameborder="0"></iframe>'
			})).show();
			Ext.getDom(tabId+"1").src = systemContext + node.attributes.link;
			this.main.setActiveTab(tab);
			loadedTabs.push(tabId);
        }else{
            this.main.setActiveTab(tab);
        }
	},
	//change的实现
	changeTab:function(tab,newtab){
		//如果存在相应树节点，就选中,否则就清空选择状态
        var nodeId = newtab.id.replace('tab-','');
	},
	closeMenuPanle:function(){
		//accordion.collapsed(true);
		//Ext.get('accordionPanel').collapsed = false;
	}
});
function logout(){
	location.href = "/logout"
}
